<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/layui/layui.js"></script>
    <!--<script src="/static/axios/axios.min.js"></script>-->
    <script src="/static/JQuery/jquery-3.3.1.min.js"></script>
</head>
<body class="layui-layout-body" style="padding: 10px">

<form class="layui-form" action="" lay-filter="stateEdit">
    <input type="hidden" id="stateid" name="stateid" />
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">状态名</label>
            <div class="layui-input-inline">
                <input type="text" name="statename" class="layui-input">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="stateSubmit">立即提交</button>
        </div>
    </div>
</form>

</body>

</html>

<script>
    //alert(localStorage.stateid);
    layui.use(['form', 'layedit', 'laydate', 'layer'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;


        if(localStorage.stateid){
            $.ajax({
                url:"/restful/find",
                type:"POST",
                data:JSON.stringify({
                    firstTable:"states",
                    wheres:[{field:"states.stateid",value:localStorage.stateid}]
                }),
                contentType: "application/json;charset=utf-8",
                dataType : "json",
                async:false,
                success : function(result) {
                    var data = result.data[0];
                    //表单input初始赋值
                    form.val('stateEdit', data);
                    //select初值
                },
                error:function(msg){
                    layer.alert(msg);
                }
            });
        }

        form.on('submit(stateSubmit)', function(data){
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            //parent.layer.close(this);
            $.ajax({
                url:"/restful/save/state",
                type:"POST",
                data:JSON.stringify([data.field]),
                contentType: "application/json;charset=utf-8",
                dataType : "json",
                async: false,
                success : function(result) {
                    parent.layer.msg("操作成功", {icon : 1});
                },
                error:function(result){
                    parent.layer.msg("操作失败", {icon : 5});
                }
            });
            parent.layer.close(index);
            return false;
        });


    });

</script>
